package com.example.demo.service;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import com.example.demo.config.ElasticSearchConfig;

import com.example.demo.utils.ElasticSearchUtils;

import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;

import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.settings.get.TransportGetSettingsAction;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.settings.Settings;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.thymeleaf.engine.IterationStatusVar;


import java.util.HashMap;
import java.util.Map;

@Service
public class IElasticSearchService{


  @Autowired
  private ElasticSearchUtils elasticSearchUtils;




  public String createIndexSettings(String index, Integer shardsNum, Integer replicasNum) throws Exception {

    try {
      //分片数及副本数
      Settings.Builder sb = Settings.builder()
              .put("index.number_of_shards", shardsNum)
              .put("index.number_of_replicas", replicasNum);
      ElasticSearchConfig elasticSearchConfig = new ElasticSearchConfig();
      //创建索引
      CreateIndexResponse response = elasticSearchUtils.transportClient().admin().indices()
              .prepareCreate(index)
              .setSettings(sb)
              .get();

    } catch (Exception e) {
      e.printStackTrace();
    }
    return "创建成功";
  }


  public String updateIndexSettings(String index, Integer replicasNum)  {



    try {
      //创建条件
      Settings.Builder sb = Settings.builder();

      //如果需要更改副本数 则加进条件  注:索引分片数在索引创建好了之后就不能调整了，只能重建索引
      sb.put("index.number_of_replicas", replicasNum);

      ActionResponse updateSettingsResponse = elasticSearchUtils.transportClient().admin().indices()
              .prepareUpdateSettings(index)
              .setSettings(sb)
              .get();

    } catch (Exception e) {
      e.printStackTrace();
    }
    return "修改成功";
  }


  public String deleteIndex(String index)  {
    try {
      ActionResponse response = elasticSearchUtils.transportClient().admin().indices()
              .prepareDelete(index)
              .get();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "删除成功";
  }


  public Map<String, Object> getIndexSettings(String index, String type){

    try {
      GetSettingsResponse response = elasticSearchUtils.transportClient().admin().indices()
              .prepareGetSettings(index)
              .get();

      Map<String, Object> map = new HashMap<>();
      for (ObjectObjectCursor<String, Settings> cursor : response.getIndexToSettings()) {
        String index2 = cursor.key;
        Settings settings = cursor.value;
        Integer shards = settings.getAsInt("index.number_of_shards", null);
        Integer replicas = settings.getAsInt("index.number_of_replicas", null);

        map.put("index", index2);
        map.put("shards", shards);
        map.put("replicas", replicas);
      }

      return map;

    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

}
